package com.doubi.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.entity.Friend;
import com.doubi.entity.Inform;
import com.doubi.entity.Tagtable;
import com.doubi.entity.Userinfo;
import com.doubi.service.FriendService;
import com.doubi.service.InformService;
import com.doubi.service.UserinfoService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@CrossOrigin("*")
@RestController
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private InformService informService;
    /**
     * 查询关注列表
     * @param userId 用户id
     * @return 关注列表
     */
    @GetMapping("/findAtt")
    public List<Friend> findAtt(Integer userId) {
        //我的关注列表
        QueryWrapper<Friend> qr = new QueryWrapper<>();
        qr.eq("userId",userId).and(i->i.eq("status",0));
        List<Friend> list = friendService.list(qr);
        //
        for (int i = 0; i < list.size(); i++) {
            int fId=list.get(i).getUserFriend();
            //获得我关注对象的用户信息
            QueryWrapper<Userinfo> q=new QueryWrapper<>();
            q.eq("userId",fId);
            Userinfo friend = userinfoService.getOne(q);
            list.get(i).setUserinfo(friend);
            if (list.get(i).getUserinfo().getSynopsis()!=null){
                if (list.get(i).getUserinfo().getSynopsis().length()>17){
                    list.get(i).getUserinfo().setSynopsis(list.get(i).getUserinfo().getSynopsis().substring(0,17)+"...");
                }
            }

            //查询我和我的关注是否互相关注
            QueryWrapper<Friend> qw = new QueryWrapper<>();
            qw.eq("userId",fId).and(j->j.eq("status",0)).and(j->j.eq("userFriend",userId));
            Friend one = friendService.getOne(qw);
            if (one != null) {
                //说明此用户与我互为关注
                list.get(i).setStatus(4);
            }
            list.get(i).getUserinfo().setHeadImg("http://192.168.43.92:9000/star/"+list.get(i).getUserinfo().getHeadImg());
        }
        return list;
    }

    /**
     * 查询粉丝列表
     * @param userId 用户id
     * @return 粉丝列表
     */
    @GetMapping("/findFans")
    public List<Friend> findFans(Integer userId) {
        QueryWrapper<Friend> qr = new QueryWrapper<>();
        qr.eq("userFriend",userId).and(i->i.eq("status",0));
        List<Friend> list = friendService.list(qr);
        for (int i = 0; i < list.size(); i++) {
            int uId=list.get(i).getUserId();
            //获得关注我的用户信息
            QueryWrapper<Userinfo> q=new QueryWrapper<>();
            q.eq("userId",uId);
            Userinfo friend = userinfoService.getOne(q);
            list.get(i).setUserinfo(friend);
            if (list.get(i).getUserinfo().getSynopsis()!=null){
                if (list.get(i).getUserinfo().getSynopsis().length()>12){
                    list.get(i).getUserinfo().setSynopsis(list.get(i).getUserinfo().getSynopsis().substring(0,12)+"...");
                }
            }
            //查询我是否关注此人
            QueryWrapper<Friend> qw = new QueryWrapper<>();
            qw.eq("userId",userId).and(j->j.eq("status",0)).and(j->j.eq("userFriend",uId));
            Friend one = friendService.getOne(qw);
            if (one != null) {
                //说明此用户与我互为关注
                list.get(i).setStatus(4);
            }
            list.get(i).getUserinfo().setHeadImg("http://192.168.43.92:9000/star/"+list.get(i).getUserinfo().getHeadImg());
        }
        return list;
    }

    @PostMapping("/addInterest")
    public String addInterest(@RequestBody Friend friend) {
        boolean save = friendService.save(friend);
        if (save) {
            return "success";
        }else {
            return "fail";
        }
    }

    @PostMapping("/delInterest")
    public String delInterest(@RequestBody Friend friend) {
        QueryWrapper<Friend> qw = new QueryWrapper<>();
        qw.eq("userFriend",friend.getUserFriend()).and(i->i.eq("userId",friend.getUserId())).and(i->i.eq("status",0));
        boolean del = friendService.remove(qw);
        if (del) {
            return "success";
        }else {
            return "fail";
        }
    }
    /**
     * 判断两人是否互相关注
     */
    @GetMapping("ifFriend")
    public Object ifFriend(Integer uid,Integer fid) {
        return friendService.insertFriend(uid,fid);
    }
    /**
     * 添加关注
     */
    @PostMapping("addattention")
    public Object addattention(Integer uid,Integer fid) {
        Friend friend = new Friend();
        friend.setUserId(uid);
        friend.setUserFriend(fid);
        friend.setStatus(0);
        return friendService.save(friend);
    }
    /**
     * 取消关注
     */
    @PostMapping("delattention")
    public Object delattention(Integer uid,Integer fid) {
        QueryWrapper<Friend> qw = new QueryWrapper<>();
        qw.eq("userFriend",fid).and(i->i.eq("userId",uid));
        return friendService.remove(qw);
    }
    /**
     * 根据名字查询用户
     */
    @GetMapping("selectUserLike")
    public Object selectUserLike(String userName) {QueryWrapper<Userinfo> qw = new QueryWrapper<>();
        qw.like("userName",userName);
        List<Userinfo> userinfos = userinfoService.list(qw);
        List<Friend> friends=new ArrayList<>();
        for (Userinfo userinfo : userinfos) {
            Friend friend = new Friend();
            friend.setUserinfo(userinfo);
            friend.setUserFriend(userinfo.getUserId());
            friends.add(friend);
        }
        return friends;
    }
    /**
     * 将@的用户发送至通知表
     */
    @PostMapping("sendInform")
    public Object sendInform(@RequestBody Map<String,Object> obj) {
        Object addUserList = obj.get("addUserList");
        Object releaseId = obj.get("releaseId");
        Integer rid=Integer.parseInt(releaseId.toString());
        List<Friend> friends = (List<Friend>) addUserList;
        List<Friend> friendList=new ArrayList<>();
       for (int i = 0; i < friends.size(); i++) {
           Friend friend= JSON.parseObject(JSON.toJSONString(friends.get(i)),Friend.class);
           friendList.add(friend);
       }
       for (Friend friend : friendList) {
           //将@的人存入通知表
           Inform inform=new Inform();
           inform.setUid(friend.getUserId());
           inform.setBuid(friend.getUserFriend());
           inform.setInformType(3);
           LocalDateTime localDateTime=LocalDateTime.now();
           DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
           String data=formatter.format(localDateTime);
           inform.setInformTime(data);
           inform.setReleaseId(rid);
           return informService.save(inform);
       }
        return false;
    }
}
